﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Globalization;
using System.Threading;
using System.Net;
using System.IO;
using System.Web.Security;
namespace Fcom.VNInsurance.Web.CultureHelper
{
    public class CultureController : Controller
    {
        protected override void ExecuteCore()
        {

            
            string cultureName = "vi-VN";
            HttpCookie cultureCookie;
            // Attempt to read the culture cookie from Request
            if (Request.Cookies["_culture"] == null)
            {
                cultureCookie = new HttpCookie("_culture");
               
                cultureName = CultureHelper.GetDefaultCulture();
                cultureCookie["lang"] = cultureName;
                Response.Cookies.Add(cultureCookie);
            }
            else
            {
                cultureCookie = Request.Cookies["_culture"];

                if (cultureCookie != null)
                {
                    cultureName = cultureCookie["lang"];
                    cultureName = CultureHelper.GetValidCulture(cultureName); // This is safe
                }
                else
                {
                    cultureName = CultureHelper.GetDefaultCulture();
                    cultureCookie["lang"] = cultureName;
                    Response.Cookies.Add(cultureCookie);
                }
            }

            // Modify current thread's cultures   
            System.Globalization.CultureInfo cultureme = new System.Globalization.CultureInfo(cultureName);
            cultureme.DateTimeFormat.ShortDatePattern = "dd/MM/yyyy";
            cultureme.DateTimeFormat.DateSeparator = "/";
            Thread.CurrentThread.CurrentCulture = cultureme;
            Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture;


            base.ExecuteCore();
        }
        private string EncodeTo64(string toEncode)
        {

            byte[] toEncodeAsBytes

                  = System.Text.Encoding.Unicode.GetBytes(toEncode);

            string returnValue

                  = System.Convert.ToBase64String(toEncodeAsBytes);

            return returnValue;

        }
        private string DecodeFrom64(string encodedData)
        {

            byte[] encodedDataAsBytes

                = System.Convert.FromBase64String(encodedData);

            string returnValue =

               System.Text.Encoding.Unicode.GetString(encodedDataAsBytes);

            return returnValue;

        }


    }
}
